package com.xgst.callnumberscreen.activity

import android.content.Intent
import android.view.View
import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.FileUtils
import com.blankj.utilcode.util.LogUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.chad.library.adapter.base.listener.OnItemLongClickListener
import com.gym.baselib.base.BaseActivity
import com.ht.medicaltreatment.R
import com.ht.medicaltreatment.adapter.LogTimeAdapter
import com.ht.medicaltreatment.view.LogDeleteDialog
import com.ht.medicaltreatment.view.XItemDecoration
import com.xgst.lib.utils.showToast
import kotlinx.android.synthetic.main.activity_log_time_layout.*
import java.io.File

//Log
class LogTimeActivity: BaseActivity(), OnItemClickListener, OnItemLongClickListener {
    override fun getLayoutId() = R.layout.activity_log_time_layout

    override fun init() {
        setTitle("日志记录")

        recyclerView.layoutManager = LinearLayoutManager(this)
        recyclerView.addItemDecoration(XItemDecoration(this,LinearLayoutManager.VERTICAL,R.drawable.shape_item_line,0))
        val logFile = LogUtils.getLogFiles()
        val adapter = LogTimeAdapter(logFile.reversed().toMutableList())
        recyclerView.adapter = adapter
        adapter.setOnItemClickListener(this)
        adapter.setOnItemLongClickListener(this)
    }

    override fun onItemClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
        val file = adapter.getItem(position) as File
        val intent = Intent(this,LogInfoActivity::class.java)
        intent.putExtra("file",file)
        startActivity(intent)
    }

    override fun onItemLongClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int): Boolean {
        LogDeleteDialog(this).showDialog {
            val file = adapter.getItem(position) as File
            val delete = FileUtils.delete(file)
            if(delete){
                adapter.removeAt(position)
            }else{
                showToast("删除失败")
            }
        }
        return true
    }

}